#include "TestDatabase.h"

void showResults(vector<vector<string> >);

int main(){
	
 	char filename[] = "test.db";
	
	Database aDatabase(filename);

	if (aDatabase.getFail() == 1){
		cout << "exiting program ..." << endl;
		exit(-1);
	}
	else{
		cout << "You are now connected to the database ...\n";

		// dummy data
		// string statement1 = "CREATE TABLE BOOKS(id INTEGER PRIMARY KEY, title TEXT, genre TEXT);"
		// string statement2 = "CREATE TABLE AUTHORS(id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT);"
		// string statement3 = "CREATE TABLE AUTHORS_BOOKS(id INTEGER PRIMARY KEY, authors_id INTEGER REFERENCES AUTHORS(id) ON DELETE CASCADE, books_id INTEGER REFERENCES BOOKS(id) ON DELETE CASCADE);";

		// string statement4 = "INSERT INTO BOOKS(id, title, type) VALUES (NULL, 'Secure Programming C/C++','Programming');";
		// string statement5 = "INSERT INTO BOOKS(id, title, type) VALUES (NULL, 'Confession of a jackass', 'Biography');";

		// string statement6 = "INSERT INTO BOOKS(id, title, type) VALUES (NULL, 'Data Structures C++', 'Programming');";
		// string statement7 = "INSERT INTO WRITERS(id, firstname, lastname) VALUES (NULL, 'Aik Chun', 'Sim');";
		// string statement8 = "INSERT INTO WRITERS(id, firstname. lastname) VALUES (NULL, 'Eugene', 'Ong');";
		// string statement9 = "INSERT INTO WRITERS(id, firstname, lastname) VALUES (NULL, 'Joel', 'Hoe');";

		// string statement10 = "INSERT INTO AUTHORS_BOOKS(id, books_id, authors_id) VALUES (NULL, 1, 1);";
		// string statement11 = "INSERT INTO AUTHORS_BOOKS(id, books_id, authors_id) VALUES (NULL, 1, 2);";
		// string statement12 = "INSERT INTO AUTHORS_BOOKS(id, books_id, authors_id) VALUES (NULL, 2, 3);";

		string statement;
		do {
			
			
			cout << "Please enter a query to the database(.quit to exit): " << endl;
			cout << "> ";
			getline(cin, statement);
			cin.clear();
			cin.sync();
			if (statement != ".quit")
			showResults(aDatabase.query(statement)) ; // function to display
		}while (statement != ".quit");
		

		
			
	}
	return 0;
}

void showResults(vector<vector<string> > results){
	
	for (int i=0;i<results.size();i++){

		vector<string>temp = results[i];

		for (int j=0;j<temp.size();j++){
			cout << temp[j] << endl;
		}
	}

}